package dao;

import entity.Orgnazition;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InstitutionalDao extends BaseDao {
    //创建连接对象
    Connection conn = null;
    //执行SQL语句
    PreparedStatement pstmt = null;
    //接收返回值
    ResultSet rs = null;

    /**
     * 查询所有信息
     */
    public List<Orgnazition> list() {
        List<Orgnazition> list = new ArrayList<>();
        try {
            conn = super.getConnection();
            String sql="select * from orgnazition order by createDate desc";
            pstmt=conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Orgnazition orgnazition = new Orgnazition(rs.getInt("id")
                        , rs.getString("name")
                        , rs.getString("principalName")
                        , rs.getInt("status")
                        , rs.getString("createDate"));
                list.add(orgnazition);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }

        return list;
    }

    /**
     *根据组织机构名称查询信息
     */
    public int selectName(String name) {
        int count=0;
        try {
            conn = super.getConnection();
            pstmt = conn.prepareStatement("select count(1) from orgnazition where name=? ");
            pstmt.setString(1,"name");
            rs = pstmt.executeQuery();
            if (rs.next()) {
                count=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return count;
    }

    /**
     * 添加
     */
    public  int add(Orgnazition orgnazition){
        int result=0;
        try {
            conn=super.getConnection();
            pstmt=conn.prepareStatement("INSERT INTO `orgnazition`( `name`, `principalName`, `status`, `createDate`) VALUES (?,?,0,?)");
            pstmt.setString(1,orgnazition.getName());
            pstmt.setString(2,orgnazition.getPrincipalName());
            pstmt.setString(3,orgnazition.getCreateDate());
            result=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return result;
    }

    /**
     * 修改状态
     */
    public  int update(int id,int status){
        int result=0;
        try {
            conn=super.getConnection();
            pstmt=conn.prepareStatement("UPDATE `orgnazition` SET `status` = ? WHERE `id` = ?");
            pstmt.setInt(1,status);
            pstmt.setInt(2,id);
            result=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return result;
    }

}
